package com.aisino.mplearning.utils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author guochuantao
 * @version 1.0
 * @description sql元数据属性转换工具包
 * @since 2024/9/6 下午4:18
 */
public class SqlMetaUtils {

    /**
     * 反转类型数据字符串获取到对应的数据类型int值
     *
     * @param typeName 数据类型字符串
     * @return int标识
     */
    public static final int getDbTypeId(String typeName) {
        switch (typeName.toUpperCase()) {
            case "CHAR":
                return 1;
            case "CLOB":
                return 2005;
            case "VARCHAR":
                return 12;
            case "LONGVARCHAR":
                return -1;
            case "SMALLINT":
                return 5;
            case "INTEGER":
            case "INT":
                return 4;
            case "BIGINT":
                return -5;
            case "NUMERIC":
                return 2;
            case "DECIMAL":
                return 3;
            case "FLOAT":
                return 6;
            case "REAL":
                return 7;
            case "DOUBLE":
                return 8;
            case "TINYINT":
                return -6;
            case "DATE":
                return 91;
            case "TIME":
                return 92;
            case "TIMESTAMP":
                return 93;
            case "BINARY":
                return -2;
            case "VARBINARY":
                return -3;
            case "BLOB":
                return 2004;
            case "LONGVARBINARY":
                return -4;
            case "BOOLEAN":
                return 16;
            case "BIT":
                return -7;
            default:
        }
        return 0;
    }

    /**
     * 根据数据库的类型转换出对应字段的长度  如果数据类型中没有对应的长度值直接返回0
     *
     * @param columnType 列的数据类型
     * @return 长度len
     */
    public static int getDbLengthByDataType(String columnType) {
        Pattern pattern = Pattern.compile("\\((\\d+)");
        Matcher matcher = pattern.matcher(columnType);
        if (matcher.find()) {
            return Integer.parseInt(matcher.group(1));
        } else {
            return 0;
        }
    }

    /**
     * postgresSQL的长度需要动态获取，根据现有样本pg的字段实际长度与三个参数相关，
     * character_maximum_length、numeric_precision、datetime_precision
     * @param resultSet 自定义sql查询集合
     * @return 字段实际长度
     */
    public static int getDbLengthForPostgres(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                int characterMaximumLength = resultSet.getInt("character_maximum_length");
                int numericPrecision = resultSet.getInt("numeric_precision");
                int datetimePrecision = resultSet.getInt("datetime_precision");
                if (numericPrecision != 0){
                    return numericPrecision;
                }
                if (characterMaximumLength != 0){
                    return characterMaximumLength;
                }
                if (datetimePrecision != 0){
                    return datetimePrecision;
                }
            }
        } catch (SQLException e) {
            return 0;
        }
        return 0;
    }
}
